﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSOCTNet.IBIZ;
using Aliyun.OSS.Sdk.JSST;

namespace JSOCTNet.BIZ
{
    public class SendFile : ISendFile
    {
        public bool Init(string config)
        {
            bool bol = false;
            try
            {
                OSSConfig ossConfig = new OSSConfig();
                ossConfig = JsonHelper.GetModel<OSSConfig>(config);
                //ossConfig.serverAddress = "https://oss-cn-shenzhen.aliyuncs.com";
                Aliyun.OSS.Sdk.JSST.Config.Ini(ossConfig.userName, ossConfig.passWord, ossConfig.serverAddress, ossConfig.bucket);
                bol = true;
            }
            catch (Exception ex)
            {
                bol = false;
            }
            return bol;
        }

        public int Send(string xmppUserName, string objJsonString)
        {
            FileDescription fileDescription = new FileDescription();
            fileDescription = JsonHelper.GetModel<FileDescription>(objJsonString);
            string fileName = fileDescription.fileName;
            string key = "{0}/{1}/{2}/{3}/{4}";
            string filename = fileName.Substring(fileName.LastIndexOf('\\') + 1);
            string[] array = filename.Split('_');
            //string PK_STR = array[2].Substring(0, 8);
            string PK_STR = fileDescription.attributes["PK_STR"].ToString();
            key = string.Format(key
                , xmppUserName
                , fileDescription.attributes["COMMAND_ID"]
                , PK_STR
                , fileDescription.attributes["RECORD_ID"]
                , fileName.Substring(fileName.LastIndexOf('\\') + 1));

            Result result = new Result();
            Framework.LogSave.PrintLog("图片上传", string.Format("图片文件上传名称：{0}，文件路径：{1}", key, fileDescription.fileName));
            int intResult = PutObjectHelp.PutObjectFromFile(key, fileDescription.fileName, out result);
            Framework.LogSave.PrintLog("图片上传", string.Format("图片文件上传结果{0} 返回值{1}", intResult, JsonHelper.GetJson(result)));
            return intResult;
        }


        public string GetPictureUri(string xmppUserName,DateTime inTime,string recordID, string fileName)
        {
            string key = "{0}/{1}/{2}/{3}/{4}";
            key = string.Format(key
                , xmppUserName
                , "NISSP_IMG_PARK_IN"
                , inTime.ToString("yyyyMMdd")
                , recordID
                , fileName.Substring(fileName.LastIndexOf('\\') + 1));

            Uri uri = PutObjectHelp.GeneratePresignedUri("", key);
            if (uri != null)
            {
                return uri.AbsoluteUri.ToString();
            }
            else
            {
                return "";
            }
        }
    }
}
